package cn.jinhe.doc.util;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.jinhe.doc.domain.Privilege;
import cn.jinhe.doc.domain.User;
@Component
public class Installer {
	@Resource
	private SessionFactory sessionFactory;
	
	@Transactional
	public void install(){
		Session session=sessionFactory.getCurrentSession();
		//============================================================
		//保存超级管理员用户
		User user=new User();
		user.setLoginName("admin");
		user.setName("超级管理员");
		user.setPassword(DigestUtils.md5Hex("admin"));
		session.save(user);
		//============================================================
		//保存权限数据
		Privilege menu,menu1,menu2,menu3,menu4,menu5,menu6;
		menu=new Privilege("系统管理",null,null);

		menu1=new Privilege("岗位管理","/role_list",menu);
		menu2=new Privilege("部门管理","/department_list",menu);

		menu3=new Privilege("用户管理","/user_list",menu);
		menu4=new Privilege("物料管理","/material_list",menu);
		menu5=new  Privilege("主机管理","/pcInfo_list",menu);
		menu6=new  Privilege("固定资产管理","/fixedAssts_list",menu);

		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		session.save(menu4);
		session.save(menu5);
		session.save(menu6);


		session.save(new Privilege("岗位管理", "/role_list", menu1));
		session.save(new Privilege("岗位删除", "/role_delete",menu1));
		session.save(new Privilege("岗位添加", "/role_add", menu1));
		session.save(new Privilege("岗位修改", "/role_edit", menu1));

		session.save(new Privilege("部门列表", "/department_list", menu2));
		session.save(new Privilege("部门删除", "/department_delete", menu2));
		session.save(new Privilege("部门添加", "/department_add", menu2));
		session.save(new Privilege("部门修改", "/department_edit", menu2));

		session.save(new Privilege("用户列表", "/user_list", menu3));
		session.save(new Privilege("用户删除", "/user_delete", menu3));
		session.save(new Privilege("用户添加", "/user_add", menu3));
		session.save(new Privilege("用户修改", "/user_edit", menu3));
		session.save(new Privilege("初始化密码", "/user_initPassword", menu3));
		
		session.save(new Privilege("物料列表", "/material_list", menu4));
		session.save(new Privilege("物料删除", "/material_delete", menu4));
		session.save(new Privilege("物料添加", "/material_add", menu4));
		session.save(new Privilege("物料修改", "/material_edit", menu4));
		
		session.save(new Privilege("主机列表", "/pcInfo_list", menu5));
		session.save(new Privilege("主机删除", "/pcInfo_delete", menu5));
		session.save(new Privilege("主机添加", "/pcInfo_add", menu5));
		session.save(new Privilege("主机修改", "/pcInfo_edit", menu5));
		
		session.save(new Privilege("固定资产列表", "/fixedAssts_list", menu6));
		session.save(new Privilege("固定资产删除", "/fixedAssts_delete", menu6));
		session.save(new Privilege("固定资产添加", "/fixedAssts_add", menu6));
		session.save(new Privilege("固定资产修改", "/fixedAssts_edit", menu6));
		// --------------------
		menu = new Privilege("知识发布", null, null);
		menu1 = new Privilege("知识库管理", "/repositoryManager_list", menu);
		menu2 = new Privilege("知识库", "/repository_list", menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);

		//---------------------
		menu = new Privilege("预案管理", null, null);
		menu1 = new Privilege("预案库", "/plan_list", menu);
		session.save(menu);
		session.save(menu1);

		session.save(new Privilege("预案刪除", "/plan_delete", menu1));
		
		session.save(new Privilege("预案库添加", "/plan_add", menu1));

		session.save(new Privilege("预案库修改", "/plan_edit", menu1));

		session.save(new Privilege("阶段信息修改", "/step_edit", menu1));

		session.save(new Privilege("阶段信息添加", "/step_add", menu1));

		//----------------------
				
		menu = new Privilege("服务管理", null, null);
		menu1 = new Privilege("服务类型管理", "/serviceManage_list", menu);
		menu2 = new Privilege("服务记录", "/service_list", menu);
		menu3 = new Privilege("用户电脑签收", "/userReceipt_list", menu);
		

		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		session.save(new Privilege("签收表删除", "/userReceipt_delete", menu3));
		session.save(new Privilege("签收表添加", "/userReceipt_add", menu3));
		session.save(new Privilege("签收表修改", "/userReceipt_edit", menu3));
		// --------------------
		menu = new Privilege("审批流转", null, null);
		menu1 = new Privilege("审批流程管理", "/processDefinition_list", menu);
		menu2 = new Privilege("申请模板管理", "/template_list", menu);
		menu3 = new Privilege("起草申请", "/flow_templateList", menu);
		menu4 = new Privilege("待我审批", "/flow_myTaskList", menu);
		menu5 = new Privilege("我的申请查询", "/flow_myApplicationList", menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		session.save(menu4);
		session.save(menu5);
	}

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Installer installer = (Installer) ac.getBean("installer");
		installer.install();
	}
}
